<template>
  <el-dialog :visible.sync="dialogComponent" :before-close="check" title="动作编辑" width="38%">
    <el-form>
      <el-form-item class="is-required" label="动作标识" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-input v-model="behaviorIdentity" style="width:200px" size="mini" type="text" placeholder="请输入动作标识" />
          </el-col>
        </el-row>
      </el-form-item>
      <el-form-item class="is-required" label="动作名称" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-input v-model="behaviorName" style="width:200px" size="mini" type="text" placeholder="请输入动作名称" />
          </el-col>
        </el-row>
      </el-form-item>
      <el-form-item class="is-required" label="积分变动" label-width="100px">
        <el-row>
          <el-col :span="24" calss="positionss">
            <el-input v-model="creditChange" style="width:200px" size="mini" type="text" placeholder="请输入积分变动" />
            <div class="remark">可输入整数</div>
          </el-col>
        </el-row>
      </el-form-item>
      <el-form-item class="is-required" label="成长值变动" label-width="100px">
        <el-row>
          <el-col :span="24" calss="positionss">
            <el-input v-model="growthChange" style="width:200px" size="mini" type="text" placeholder="请输入成长值变动" />
            <div class="remark">可输入整数</div>
          </el-col>
        </el-row>
      </el-form-item>
      <el-row>
        <el-col :span="24" calss="positionss">
          <el-form-item label="备注" label-width="100px">
            <el-input v-model="desc" type="textarea" style="width:200px" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item class="btn">
        <el-row type="flex" justify="center">
          <el-col :span="6">
            <el-button
              type="primary"
              size="mini"
              round
              @click="saveEdit"
            >保存</el-button>
          </el-col>
          <el-col :span="6">
            <el-button
              size="mini"
              type="primary"
              round
              plain
              @click="check"
            >取消</el-button>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { zeroNumss, numAndWord } from '@/utils/validate'
import { save, editAction } from '@/api/actionManage'
import secret from '@/utils/secret'
export default {
  props: {
    editDialog: {
      type: Boolean,
      default: false
    },
    scope: {
      type: Object,
      default: null
    },
    platId: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      dialogComponent: false,
      behaviorIdentity: '',
      behaviorName: '',
      creditChange: '',
      growthChange: '',
      desc: '',
      id: '',
      statuss: false
    }
  },
  watch: {
    scope(newVal) {
      // console.log(newVal)
      this.id = newVal.id
      this.behaviorIdentity = newVal.behaviorIdentity
      this.behaviorName = newVal.behaviorName
      this.creditChange = newVal.creditChange
      this.growthChange = newVal.growthChange
      this.desc = newVal.desc
    },
    editDialog(newVal) {
      // console.log(newVal)
      this.dialogComponent = newVal
    }
  },
  mounted() {
    // console.log(this.editDialog)
  },
  methods: {
    // 回调
    check() {
      this.$emit('closeAction', this.statuss)
      console.log(status)
      this.statuss = false
      this.dialogComponent = false
    },
    saveEdit() {
      if (!this.behaviorIdentity) {
        this.$message({
          message: '动作标识不可为空',
          type: 'error'
        })
        return
      }
      if (!numAndWord(this.behaviorIdentity)) {
        this.$message({
          message: '动作标识必须填写字母或数字',
          type: 'error'
        })
        return
      }
      if (!this.behaviorName) {
        this.$message({
          message: '动作名称不可为空',
          type: 'error'
        })
        return
      }
      if (!zeroNumss(this.creditChange)) {
        this.$message({
          message: '积分必须填写整数',
          type: 'error'
        })
        return
      }
      if (!zeroNumss(this.growthChange)) {
        this.$message({
          message: '成长值变动必须填写整数',
          type: 'error'
        })
        return
      }
      // 新增 editAction
      if (this.id === '') {
        var data = {
          platformId: this.platId,
          behaviorIdentity: this.behaviorIdentity,
          behaviorName: this.behaviorName,
          creditChange: this.creditChange,
          growthChange: this.growthChange,
          desc: this.desc
        }
        console.log(data)
        save(secret.Encrypt(data)).then(response => {
          this.$message({
            message: response.resultDesc,
            type: 'success'
          })
          this.statuss = true
          this.check()
        })
      } else {
        this.desc = this.desc === null ? '' : this.desc
        var datas = {
          id: this.id,
          platformId: this.platId,
          behaviorIdentity: this.behaviorIdentity,
          behaviorName: this.behaviorName,
          creditChange: this.creditChange,
          growthChange: this.growthChange,
          desc: this.desc
        }
        console.log(datas)
        editAction(secret.Encrypt(datas)).then(response => {
          this.$message({
            message: response.resultDesc,
            type: 'success'
          })
          this.statuss = true
          this.check()
        })
      }
    }
  }
}
</script>

<style scoped>
.positionss{
  position: relative
}
.remark{
  position: absolute;
  left: 220px;
  top: 0;
  color: red
}
.btn{
    margin-bottom: -20px !important;
    padding-bottom: 10px;
  }
</style>
